package cn.wsalix.admin.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.anlaser.service.impl.ParentServiceImpl;
import cn.wsalix.admin.entity.MenuPermit;
import cn.wsalix.admin.entity.RequiresPermit;
import cn.wsalix.admin.entity.RequiresRole;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.form.RoleForm;
import cn.wsalix.admin.repository.MenuPermitRepository;
import cn.wsalix.admin.repository.RequiresPermitRepository;
import cn.wsalix.admin.repository.RequiresRoleRepository;
import cn.wsalix.admin.repository.RolePermitRepository;
import cn.wsalix.admin.repository.RoleRepository;
import cn.wsalix.admin.repository.UserRoleRepository;
import cn.wsalix.admin.service.RoleService;
import cn.wsalix.admin.service.UserService;
import cn.anlaser.utils.SearchFilter;

@Service("roleService")
public class RoleServiceImpl extends
		ParentServiceImpl<RoleForm, RoleRepository, SysRole> implements
		RoleService {
	@Autowired
	private RoleRepository roleRepository;
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleRepository userRoleRepository;
	@Autowired
	private RolePermitRepository rolPermitRepository;
	@Autowired
	private MenuPermitRepository menuPermitRepository;

	@Autowired
	private RequiresRoleRepository requiresRoleRepository;

	@Autowired
	private RequiresPermitRepository requiresPermitRepository;

	@Override
	public RoleRepository getRepository() {
		return roleRepository;
	}

	@Override
	public String module() {
		return "/admin/role";
	}

	@Override
	public List<SysRole> findRolesByUserId(Long userId) {
		return userRoleRepository.findRolesByUserId(userId);
	}

	@Override
	public List<MenuPermit> findPermitsByUserId(Long userId) {
		return rolPermitRepository.findPermsByUserId(userId);
	}

	@Override
	public Class<SysRole> getEntityClazz() {
		return SysRole.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(RoleForm form) {
		Map<String, SearchFilter> filters = buildFilter();
		/*
		 * if (form.getParentId() != null && form.getParentId() != 0) {
		 * filters.put("parent.id", new SearchFilter("parent.id", Operator.EQ,
		 * form.getParentId())); }
		 */

		return filters;
	}

	@Override
	public List<SysRole> findUnRolesByUserId(Long userId) {
		return roleRepository.findUnRolesByUserId(userId);
	}

	@Override
	public List<SysRole> findRolesByMenuId(Long menuId) {
		return roleRepository.findRolesByMenuId(menuId);
	}

	@Override
	public List<SysRole> findUnRolesByMenuId(Long menuId) {
		return roleRepository.findUnRolesByMenuId(menuId);
	}

	@Override
	public void upUserRole(Long roleId) {
		List<SysUser> users = userService.findUsersByRoleId(roleId);
		for (SysUser user : users) {
			userService.updateUserRole(user.getId());
		}
	}

	@Override
	public List<MenuPermit> findAllMenuPermit() {
		return menuPermitRepository.findAll();
	}

	@Override
	public SysRole findByCodeOne(String code) {
		logger.info(code);
		List<SysRole> resLst = findsByCode(code);
		if (resLst.size() != 1) {
			if (resLst.size() > 1) {
				logger.error(code + " : size() > 1 [" + resLst.size() + "]");
				return resLst.get(0);
			}
			logger.error(code + " : size() =0 ");
			return null;
		} else {
			return resLst.get(0);
		}
	}

	@Override
	public SysRole findByNameOne(String name) {
		logger.info(name);
		List<SysRole> resLst = findsByName(name);
		if (resLst.size() != 1) {
			if (resLst.size() > 1) {
				logger.error(name + " : size() > 1 [" + resLst.size() + "]");
				return resLst.get(0);
			}
			logger.error(name + " : size() =0 ");
			return null;
		} else {
			return resLst.get(0);
		}
	}

	@Override
	public List<SysRole> findsByName(String name) {
		return roleRepository.findByName(name);
	}

	@Override
	public List<SysRole> findsByCode(String code) {
		return roleRepository.findByCode(code);
	}

	@Override
	public List<RequiresRole> findRequiresRoles() {
		return requiresRoleRepository.findAll();
	}

	@Override
	public List<RequiresPermit> findRequiresPermits() {
		return requiresPermitRepository.findAll();
	}

}
